import 'dart:async';

import 'package:dio/dio.dart';

/**
 * 名称：我的页面
 */
import 'package:flutter/material.dart';
import 'package:flutter_todo_2019/common/application.dart';
import 'package:flutter_todo_2019/event/tab_change_event.dart';
import 'package:flutter_todo_2019/event/mine_msg_count_event_new.dart';
import 'package:flutter_todo_2019/page/base/base_widget.dart';
import 'package:flutter_todo_2019/page/callback/MyCallback.dart';
import 'package:flutter_todo_2019/page/setting/setting_screen.dart';
import 'package:flutter_todo_2019/page/utils/GlobalInfo.dart';
import 'package:flutter_todo_2019/page/utils/item_utils.dart';
import 'package:flutter_todo_2019/page/utils/route_util.dart';
import 'package:flutter_todo_2019/page/utils/sp_util.dart';
import 'package:flutter_todo_2019/page/utils/theme_util.dart';
import 'package:flutter_todo_2019/page/utils/toast_util.dart';
import 'package:flutter_todo_2019/page/utils/utils.dart';
import 'package:flutter_todo_2019/res/colors.dart';
import 'package:flutter_todo_2019/test/anim/MyFadeTest.dart';
import 'package:flutter_todo_2019/test/list/list/movie_list_page.dart';

import 'MIneEntry.dart';

///数据源
final GlobalInfo globalInfo = new GlobalInfo();

///我的
class MinePageNew extends BaseWidget {
  final title;

  MinePageNew({Key key, this.title});

  @override
  BaseWidgetState<BaseWidget> attachState() {
    // TODO: implement attachState
    return ChildState(this.title);
  }
}

class ChildState extends BaseWidgetState<MinePageNew> implements MyCallback {
  String title;

  ChildState(this.title);

  String msgFlagsAllCount = "0";
  String msgNoFinshCount = "0";
  String msgFlagsFinshCount = "0";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print("开始");
    height=50;
    initCount();
    this.registerRefreshEvent();
  }

  @override
  AppBar attachAppBar() {
    return AppBar(
      elevation: 0, // 隐藏阴影
      title: null,
      actions: <Widget>[
        new IconButton(
            icon: ImageIcon(
              AssetImage(Utils.getImgPath("icon_more")),
              color: Colors.white,
            ),
            onPressed: () {
              RouteUtil.jumpPage(context, new SettingScreen());
            }),
      ],
    );
  }

  StreamSubscription subscription;

  /// 注册刷新TODO事件
  void registerRefreshEvent() {
    subscription = Application.eventBus.on<CountMsgNewEvent>().listen((event) {
      setState(() {
        initCount();
      });
    });
  }

  void initCount() {
    msgNoFinshCount = SPUtil.getString(GlobalInfo.MSG_NOFINSH_COUNT, defValue: '0');
    msgFlagsFinshCount = SPUtil.getString(GlobalInfo.MSG_FLAGS_FINSH_COUNT, defValue: '0');
    int noFinsh = int.parse(msgNoFinshCount);
    int finsh = int.parse(msgFlagsFinshCount);
    msgFlagsAllCount = (noFinsh + finsh).toString();
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
//    addStr();
    showContent();
  }

  @override
  Widget attachContentWidget(BuildContext context) {
    // TODO: Dart的Futures可以管理IO,不用担心线程死锁等问题
//    头部View
    var head = new Container(
      height: 50,
      width: 50,
      alignment: Alignment.center,
      margin: const EdgeInsets.fromLTRB(15.0, 20.0, 15, 20),
      child: Image.network(
        globalInfo.headUrl,
      ),
      decoration: new BoxDecoration(
        borderRadius: new BorderRadius.circular(5.0),
        color: ThemeUtils.currentThemeColor,
      ),
    );

    var ClipHead = Container(
      padding: EdgeInsets.fromLTRB(0, 8, 0, 6),
      child: ClipOval(
        child: SizedBox(
          width: 60,
          height: 60,
          child: Image.network(globalInfo.headUrl),
        ),
      ),
    );
//    进度Item View
    var myItemToDoChildRootView = new Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        getMyToDoItemChildNewView(msgFlagsAllCount + "个", "FLAGS", 0),
        getMyToDoItemChildNewView(msgFlagsFinshCount + "个", "已完成", 1),
        getMyToDoItemChildNewView(msgNoFinshCount + "个", "未完成", 0),
      ],
    );

    var headTopView = Container(
        alignment: Alignment.center,
        child: new Column(
//      crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            ClipHead,
            Opacity(
                opacity: 0.8,
                child: new Padding(
                  padding: new EdgeInsets.fromLTRB(3.0, 2.0, 0, 0),
                  child: new Text(
                    globalInfo.userName,
                    style: new TextStyle(
                      fontSize: 17.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.black,
                    ),
                  ),
                )),
            Opacity(
                opacity: 0.5,
                child: new Padding(
                  padding: new EdgeInsets.fromLTRB(3.0, 8.0, 0, 25),
                  child: new Text(
                    globalInfo.bottomText,
                    style: new TextStyle(
                      fontSize: 14.0,
                      color: Colors.black,
                    ),
                  ),
                )),
            myItemToDoChildRootView
          ],
        ));

    var topRootBgView = Container(
      decoration: BoxDecoration(
          color: ThemeUtils.currentThemeColor,
          borderRadius: BorderRadius.only(
              bottomRight: Radius.circular(70),
              bottomLeft: Radius.circular(70))),
      child: headTopView,
    );

    var bottomCard = new Container(
        height: 50.0,
        alignment: Alignment.center,
        margin: const EdgeInsets.fromLTRB(20.0, 50, 20, 0),
        decoration: new BoxDecoration(
          borderRadius: new BorderRadius.circular(10.0),
          color: Colours.app_mime_bottom_bt_color,
        ),
        child: new Text(
          "我的清单生活有效更有料",
          style: new TextStyle(
              fontSize: 16.0, color: Colours.app_mime_bottom_bt_text_color),
        ));

    var itemRootView = new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          topRootBgView,
          ItemUtils.getCardBgView(context, "2019，这是我持续成长的第10天", "222", this),
          ItemUtils.getItemCustomView(
              "觉得不错？推荐给好友", Icons.chevron_right, context, this),
          new Padding(
            padding: new EdgeInsets.fromLTRB(0, 8.0, 0, 8.0),
            child: new Divider(height: 2.0),
          ),
          ItemUtils.getItemCustomView(
              "意见反馈", Icons.chevron_right, context, this),
          bottomCard,
        ]);

    return itemRootView;
  }

  @override
  void onClickErrorWidget() {
    // TODO: implement onClickErrorWidget
    T.show(msg: "Erro");
  }

//跳转其他界面
  jumpListPage(BuildContext context) {
    RouteUtil.jumpPage(context, new MovieListPage());
  }

  //点击了Icon
  clickImg(mContext, String showFinalText) {
    T.show(msg: showFinalText);
  }

  //跳转其他界面
  jumpAnimPage(BuildContext context) async {
    Navigator.of(context)
        .push(new MaterialPageRoute(builder: (BuildContext context) {
      return new MyFadeTest(
        title: "动画简单测试",
      );
      ;
    }));
  }

  changeTab(int index) {
    Application.eventBus.fire(new TabChangeEvent(index));
  }

  getMyToDoItemChildNewView(itemKey, itemValue, int index) {
    Column itemView = new Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Opacity(
            opacity: 0.8,
            child: new Padding(
              padding: new EdgeInsets.fromLTRB(25.0, 10.0, 18.0, 0),
              child: new Text(
                itemKey,
                style: new TextStyle(
                  fontSize: 20.0,
                  fontWeight: FontWeight.bold,
                  color: Colors.black,
                ),
              ),
            )),
        Opacity(
            opacity: 0.8,
            child: new Padding(
              padding: new EdgeInsets.fromLTRB(25.0, 8.0, 18.0, 40),
              child: new Text(
                itemValue,
                style: new TextStyle(
                  fontSize: 15.0,
                  color: Colors.black,
                ),
              ),
            ))
      ],
    );
    return new InkWell(
      onTap: () {
        changeTab(index);
      },
      child: itemView,
    );
  }

  @override
  void onCallback(String result) {
    // TODO: implement onCallback
  }
}
